import Cookies from "js-cookie";
const Utils = {
  /**
   * @description 判断是不是字符串
   * @param {any} o  需要验证的参数
   * @return 返回ture flase
   */
  isObject(o: any) {
    return Object.prototype.toString.call(o).slice(8, -1) === "Object";
  },
  /**
   * @description 判断是不是对象
   * @param {any} o  需要验证的参数
   * @return 返回ture flase
   */
  isString(o: any) {
    return Object.prototype.toString.call(o).slice(8, -1) === "String";
  }
};
/**
  *@category Cookie的操作方法
  * @description Cookie的设置、获取、删除、清空
  * @param {key、data、option} o  Cookie的key、value、配置项
  * @example  Cookie.set()、Cookie.get()、Cookie.remove()、Cookie.clear()
  */
const Cookie = {
  set: (key: any, data: string, option: any) => {
    const { expires = 1, path = "/", domain = "", secure = false } = option || {};
    if (Utils.isObject(data)) {
      data = JSON.stringify(data);
    }
    Cookies.set(key, data, { expires, path, domain, secure });
  },
  get: (key: any) => {
    let _val = Cookies.get(key) || null;
    if (Utils.isString(_val)) {
      try {
        _val = JSON.parse(_val);
      } catch (error) {
        console.error(error);
      }
    }
    return _val;
  },
  clear: () => {
    document.cookie = "";
  },
  remove: (key: any, option: any) => {
    const { path = "/", domain = "" } = option || {};
    Cookies.remove(key, { path, domain });
  }
};

export default Cookie
